import ajax from './ajax'

const _processDownload = function(res, filename) {
  let link = document.createElement('a');
  let url = window.URL.createObjectURL(res.data);
  link.href = url;
  link.download = filename;
  link.click();
  setTimeout(function(){
    try {
      document.body.removeChild(link);
      window.URL.revokeObjectURL(url);
    }  catch(e) {}
  }, 100);
}

export default {

  // ================= PLUS-SETTING

  plusOnline(eventId, onSuccess) {
    ajax.post('/douban/events/' + eventId + '/owner?owner=plus-home')
        .then(onSuccess);
  },

  plusOffline(eventId, onSuccess) {
    ajax.post('/douban/events/' + eventId + '/owner?owner=joearde')
        .then(onSuccess);
  },

  // ================= SETTING
  duplicateSetting(eventId, onSuccess) {
    ajax.post('/douban/events/' + eventId + '/setting/duplicate')
        .then(onSuccess);
  },

  loadSettingMovies(eventId, onSuccess) {
    ajax.get('/douban/events/' + eventId + '/setting/movies')
        .then(onSuccess);
  },

  loadSettingActors(eventId, onSuccess) {
    ajax.get('/douban/events/' + eventId + '/setting/actors')
        .then(onSuccess);
  },

  insertSettingMovie(eventId, movieId, movieName, onSuccess) {
    ajax.post('/douban/events/' + eventId + '/setting/movies', {
          "movieId" : movieId,
          "movieName": movieName
        }).then(onSuccess);
  },

  insertSettingActor(eventId, actorId, actorName, onSuccess) {
    ajax.post('/douban/events/' + eventId + '/setting/actors', {
          "actorId" : actorId,
          "actorName": actorName
        }).then(onSuccess);
  },

  deleteSettingMovie(eventId, movieId, onSuccess) {
    ajax.post('/douban/events/' + eventId + '/setting/movies/del', {
          "movieId" : movieId
        }).then(onSuccess);
  },

  deleteSettingActor(eventId, actorId, onSuccess) {
    ajax.post('/douban/events/' + eventId + '/setting/actors/del', {
          "actorId" : actorId
        }).then(onSuccess);
  },

  // ================= 
  refreshRating(eventId, onSuccess, onError) {
    ajax.post('/spider/douban/task/event-rating?id=' + eventId)
        .then(onSuccess)
        .catch(onError);
  },

  loadEvents(offset, limit, onSuccess) {
    ajax.get('/douban/events?offset=' + offset + "&limit=" + limit)
        .then(onSuccess);
  },

  loadEventUsers(eventId, offset, onSuccess) {
    ajax.get('/douban/events/' + eventId + '/users?limit=5000&offset=' + offset)
        .then(onSuccess);
  },

  loadEventPlayers(eventId, onSuccess) {
    ajax.get('/douban/events/' + eventId + '/players')
        .then(onSuccess);
  },

  loadEventCheckers(eventId, onSuccess) {
    ajax.get('/douban/events/' + eventId + '/checkers')
        .then(onSuccess);
  },

  loadGroupEventCheckers(movieId, onSuccess) {
    ajax.get('/douban/events/movie-group/' + movieId + '/checkers')
        .then(onSuccess);
  },

  loadGroupEvent(movieId, onSuccess) {
    ajax.get('/douban/events/movie-group/' + movieId)
        .then(onSuccess);
  },

  loadEvent(eventId, onSuccess) {
    ajax.get('/douban/events/' + eventId)
        .then(onSuccess);
  },

  checkPlayer(eventId, user, onSuccess) {
    ajax.post('/douban/events/'+eventId+'/check', {
          "id" : user.id,
          "checked": 1
    })
    .then(onSuccess)
    .catch(err=>{console.log(err)});
  },

  confirmPlayer(eventId, user, status, onSuccess) {
    ajax.post('/douban/events/'+eventId+'/confirm?phone=' + user.phone + "&status=" + status)
    .then(onSuccess)
    .catch(err=>{console.log(err)});
  },

  blackPlayer(eventId, user, cause, blockDays, onSuccess) {
    ajax.post('/douban/events/'+eventId+'/check', {
      "id" : user.id,
      "checked": -1,
      "blockDays":blockDays,
      "tag":cause
    })
    .then(onSuccess)
    .catch(err=>{console.log(err)});
  },

  judgeComment(eventId, user, up, onSuccess) {
    ajax.post('/douban/events/'+eventId+'/judge', {
      "id" : user.id,
      "judge": up?1:-1
    })
    .then(onSuccess)
    .catch(err=>{console.log(err)});
  },

  restorePlayer(eventId, user, onSuccess) {
    ajax.post('/douban/events/'+eventId+'/check', {
      "id" : user.id,
      "checked": 0
    })
    .then(onSuccess)
    .catch(err=>{console.log(err)});
  },

  addPlayers(eventId, ids, onSuccess) {
    ajax.post('/douban/events/'+eventId+'/players', {
                  "ids" : ids,
                  "picked": true
             })
        .then(onSuccess)
        .catch(err=>{console.log(err)});
  },

  addPlayer(eventId, user, onSuccess) {
    ajax.post('/douban/events/'+eventId+'/player', {
                  "id" : user.id,
                  "phone": user.phone?user.phone:'',
                  "picked": true
             })
        .then(onSuccess)
        .catch(err=>{console.log(err)});
  },

  addChecker(eventId, user, onSuccess) {
    ajax.post('/douban/events/'+eventId+'/checker', {
                  "id" : user.id
             })
        .then(onSuccess)
        .catch(err=>{console.log(err)});
  },

  removePlayer(eventId, user, onSuccess) {
    ajax.post('/douban/events/'+eventId+'/player', {
          "id" : user.id,
          "picked": false
    })
    .then(onSuccess)
    .catch(err=>{console.log(err)});
  },

  uploadExcelSign(eventId, file, onSuccess) {
    let fd = new FormData();
    fd.append('file', file);
    ajax.post('/douban/events/'+eventId+'/excel-sign',fd,{headers:{'Content-Type':'multipart/form-data'}})
        .then(onSuccess);
  },

  uploadExcelPlay(eventId, file, onSuccess) {
    let fd = new FormData();
    fd.append('file', file);
    ajax.post('/douban/events/'+eventId+'/excel-play',fd,{headers:{'Content-Type':'multipart/form-data'}})
        .then(onSuccess);
  },

  downloadExcelPlay(eventId, filename) {
    ajax({
      url:'/douban/events/' + eventId + '/excel-play',
      method:'GET',
      responseType:'blob'
    }).then(res=>{
      _processDownload(res, filename);
    });
  },

  downloadExcelSign(eventId, filename) {
    ajax({
      url:'/douban/events/' + eventId + '/excel-sign',
      method:'GET',
      responseType:'blob'
    }).then(res=>{
      _processDownload(res, filename);
    });
  },

  downloadExcelAdvanceSign(eventId, filename) {
    ajax({
      url:'/douban/events/' + eventId + '/excel-sign-advance',
      method:'GET',
      responseType:'blob'
    }).then(res=>{
      _processDownload(res, filename);
    });
  },

  downloadExcelRated(eventId, filename) {
    ajax({
      url:'/douban/events/' + eventId + '/excel-rated',
      method:'GET',
      responseType:'blob'
    }).then(res=>{
      _processDownload(res, filename);
    });
  },

  downloadExcelNotRated(eventId, filename) {
    ajax({
      url:'/douban/events/' + eventId + '/excel-not-rated',
      method:'GET',
      responseType:'blob'
    }).then(res=>{
      _processDownload(res, filename);
    });
  }, 

  favEvent(eventId, isFav, onSuccess) {
    ajax.post('/douban/events/' + eventId + (isFav?'/fav':'/unfav'))
        .then(onSuccess)
        .catch(err=>{console.log(err)});
  },

  managerEvent(eventId, isManager, onSuccess) {
    ajax.post('/douban/events/' + eventId + (isManager?'/manager':'/unmanager'))
        .then(onSuccess)
        .catch(err=>{console.log(err)});
  },

  loadEventManager(eventId, onSuccess) {
    ajax.get('/douban/events/' + eventId + '/manager')
        .then(onSuccess);
  },

  loadUserEventSeat(eventId, user, onSuccess) {
    ajax.get('/douban/events/' + eventId + '/seat?userId=' + user.id)
        .then(onSuccess);
  },

  selectUserEventSeat(eventId, user, seatName, onSuccess, onError) {
    ajax.post('/douban/events/' + eventId + '/seat', {
      "id" : user.id,
      "seat": seatName
    })
    .then(onSuccess)
    .catch(onError);
  },

  miniNotify(eventId, onSuccess) {
    ajax.post('/douban/events/' + eventId + '/mini-notify')
        .then(onSuccess)
        .catch(err=>{console.log(err)});
  },

  updateCheckTime(eventId, interval, onSuccess) {
    ajax.post('/douban/events/' + eventId + '/check-time?interval=' + interval)
        .then(ret=>{})
        .catch(err=>{console.log(err)});
  }
};